package org.dromara.manu.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.manu.domain.vo.ManuChangeMasterVo;
import org.dromara.manu.domain.bo.ManuChangeMasterBo;
import org.dromara.manu.service.IManuChangeMasterService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 变更主数据
 *
 * @author Bin Liu
 * @date 2024-09-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/manu/changeMaster")
public class ManuChangeMasterController extends BaseController {

    private final IManuChangeMasterService manuChangeMasterService;

    /**
     * 查询变更主数据列表
     */
    @SaCheckPermission("manu:changeMaster:list")
    @GetMapping("/list")
    public TableDataInfo<ManuChangeMasterVo> list(ManuChangeMasterBo bo, PageQuery pageQuery) {
        return manuChangeMasterService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出变更主数据列表
     */
    @SaCheckPermission("manu:changeMaster:export")
    @Log(title = "变更主数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ManuChangeMasterBo bo, HttpServletResponse response) {
        List<ManuChangeMasterVo> list = manuChangeMasterService.queryList(bo);
        ExcelUtil.exportExcel(list, "变更主数据", ManuChangeMasterVo.class, response);
    }

    /**
     * 获取变更主数据详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("manu:changeMaster:query")
    @GetMapping("/{id}")
    public R<ManuChangeMasterVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(manuChangeMasterService.queryById(id));
    }

    /**
     * 新增变更主数据
     */
    @SaCheckPermission("manu:changeMaster:add")
    @Log(title = "变更主数据", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ManuChangeMasterBo bo) {
        return toAjax(manuChangeMasterService.insertByBo(bo));
    }

    /**
     * 修改变更主数据
     */
    @SaCheckPermission("manu:changeMaster:edit")
    @Log(title = "变更主数据", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ManuChangeMasterBo bo) {
        return toAjax(manuChangeMasterService.updateByBo(bo));
    }

    /**
     * 删除变更主数据
     *
     * @param ids 主键串
     */
    @SaCheckPermission("manu:changeMaster:remove")
    @Log(title = "变更主数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(manuChangeMasterService.deleteWithValidByIds(List.of(ids), true));
    }
}
